package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import easier.app.x.view.EditInputManager;
import easier.app.x.view.FocusTextWatcher;
import easier.app.x.view.FocusTextWatcher.ClearFocusTextWatcher;
import easier.log.logger.Logger;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.subjects.PublishSubject;
import io.reactivex.rxjava3.subjects.Subject;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

public class MainActivity102 extends AppCompatActivity {

    private EditText input;
    private ImageView clear;
    private EditText input2;
    private ImageView clear2;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity102.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main102 );
        initView();
    }

    private void initView() {

        input = (EditText) findViewById( R.id.input );
        clear = (ImageView) findViewById( R.id.clear );

        input2 = (EditText) findViewById( R.id.input2 );
        clear2 = (ImageView) findViewById( R.id.clear2 );

        EditInputManager.setMaxLength( input, 5 );
        EditInputManager.setInputNumber( input );
        DebounceClearFocusTextWatcher watcher = new NumberDebounceClearFocusTextWatcher();
        watcher.setClear( clear );
        FocusTextWatcher.setTo( watcher, input );

        EditInputManager.setMaxLength( input2, 10 );
        EditInputManager.setInputNumberDecimal( input2, 2 );
        DebounceClearFocusTextWatcher watcher2 = new NumberDebounceClearFocusTextWatcher();
        watcher2.setClear( clear2 );
        FocusTextWatcher.setTo( watcher2, input2 );
    }

    public static class NumberDebounceClearFocusTextWatcher extends DebounceClearFocusTextWatcher {

        @Override
        protected void onDebounce( CharSequence charSequence ) {

            if ( TextUtils.isEmpty( charSequence ) ) {
                return;
            }
            BigDecimal decimal = new BigDecimal( charSequence.toString() );
            String s = decimal.toString();
            if ( !TextUtils.equals( s, charSequence ) ) {
                getEditText().setText( s );
                getEditText().setSelection( s.length() );
            }
        }

        @Override
        public void afterTextChanged( Editable s ) {

            super.afterTextChanged( s );
            Logger.i( s.toString() );
        }
    }

    public static class DebounceClearFocusTextWatcher extends ClearFocusTextWatcher {

        public Subject< CharSequence > mSubject = PublishSubject.create();
        private Disposable mSubscribe;

        @Override
        public void setClear( View clear ) {

            super.setClear( clear );

            if ( mSubscribe != null ) {
                mSubscribe.dispose();
            }
            mSubscribe = mSubject.debounce( 900, TimeUnit.MILLISECONDS )
                .observeOn( AndroidSchedulers.mainThread() )
                .subscribe( new Consumer< CharSequence >() {
                    @Override
                    public void accept( CharSequence charSequence ) throws Throwable {

                        onDebounce( charSequence );
                    }
                } );
        }

        @Override
        protected void afterFocusTextChanged( EditText editText, CharSequence s, boolean hasFocus ) {

            super.afterFocusTextChanged( editText, s, hasFocus );
            mSubject.onNext( s );
        }

        protected void onDebounce( CharSequence charSequence ) {

        }
    }
}